package com.skycong.hello77.activity.recycerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.skycong.hello77.R;
import com.skycong.hello77.adapter.RecyclerAdapter;
import com.skycong.hello77.entity.ApkEntity;

import java.util.ArrayList;
import java.util.List;

public class RecycerViewActivity extends Activity implements OnLoadMoreListener,OnRefreshListener {
    private SwipeToLoadLayout swipeToLoadLayout;
    private RecyclerView swipe_target;
    private RecyclerAdapter reAdapter;
    List<ApkEntity> dataList  = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycerview);
        initView();
        swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
       //上拉刷新
        swipeToLoadLayout.setOnRefreshListener(this);
        //上拉加载滑动事件
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }


    @Override
    public void onLoadMore() {
             swipeToLoadLayout.postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     ApkEntity updata=new ApkEntity();
                     updata.setProduct_spec("shabiziji");
                     updata.setType(2);
                     dataList.add(updata);
                     reAdapter.addList(dataList);
                     reAdapter.notifyDataSetChanged();

                     //是否设置上拉加载中
                     swipeToLoadLayout.setLoadingMore(false);
                 }
             },2000);
    }

    @Override
    public void onRefresh() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(false);

            }
        },2000);
    }
    public void initView(){
        swipe_target = (RecyclerView) findViewById(R.id.swipe_target);
        //设置布局管理器方法，实现RecyclerView布局里面的内容显示方式
        //参数1，上下文，2，布局方式 3，布尔值是否反转，不反转，从上到下加载
        swipe_target.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL,false));
         reAdapter = new RecyclerAdapter(this);
         swipe_target.setAdapter(reAdapter);
         initData();
        Log.i("log","有没有数据");
    }
    private void initData() {

        for(int i = 0;i<20;i++){
            int type = (int) (Math.random()*2+1);
            ApkEntity data = new ApkEntity();
            data.setCustom_name("王小强");
            data.setCustom_address("武汉市洪山区");
            data.setCustom_distance(800);
            data.setCustom_phone("15586780450");
            data.setProduct_name("好吃点好吃点");
            data.setProduct_num("12件");
            data.setProduct_num_title("哈哈哈");
            data.setProduct_price("价格：200.00");
            data.setProduct_spec("规格：1瓶（1/6件）");
            data.setType(type);
            Log.i("i","type"+data);
            Log.i("i","data"+dataList);
            dataList.add(data);
        }

         Log.i("log","li"+dataList);
        reAdapter.addList(dataList);
        reAdapter.notifyDataSetChanged();//通知UI去刷新这个页面
        Log.i("log","进行到这里了");
    }

    public void loadMoreData(){


    }
}
